// 添加事件的兼容写法
function addEvent(el, type, fn) {
  if (el.addEventListener) {
    el.addEventListener(type, fn, false)
  } else if (el.attachEvent) {
    el.attachEvent('on' + type, function () {
      handle.call(el)
    })
  } else {
    el['on' + type] = fn
  }
}

// 获取元素的所有子节点
function elemChildren(node) {
  var temp = {
      length: 0,
      splice: Array.prototype.splice,
    },
    len = node.childNodes.length
  for (var i = 0; i < len; i++) {
    var childItem = node.childNodes[i]
    // nodeType == 1 代表根级子节点
    if (childItem.nodeType == 1) {
      temp[temp.length] = childItem
      temp['length']++
    }
  }
  return temp
}

// 获取node元素的第n层父元素
function elemChildren(node) {
  var type = typeof node
  if (type === 'undefined') {
    return node.parentNode
  } else if (n <= 0 || type !== 'number') {
    return undefined
  }
  // 循环向上查找
  while (n) {
    node = node.parentNode
    n--
  }
  return node
}
